<?
	require_once( 'core_DataBaseConnect.php' );
	require_once( 'core_ErrorCode.php' );
	require_once( 'core_Response.php' );
	require_once( 'dao_MemberDAO.php' );
	require_once( 'dao_ModelDAO.php' );
	require_once( '_configs.php' );
	
	class ServiceConfigure
	{
		// 공지사항 등록
		public static function insertServerNotice( DataBaseConnect $con
													,$serverLock
													,$start
													,$end
													,$message
													,$ownerID )
		{
			$query = 'INSERT INTO '.configs::TABLE_SERVERNOTICE.'
						(
							`SERVER_LOCK`,
							`START`,
							`END`,
							`MESSAGE`,
							`OWNER_ID`,
							`REGIST_DATE`
						)
						VALUES'."
						(
							'$serverLock',
							'$start',
							'$end',
							'$message',
							'$ownerID',
							NOW()
						);";
			return $con->query( $query );		
		}
		// 공지사항 수정
		public static function updateServerNotice( DataBaseConnect $con
													,$seq
													,$serverLock
													,$start
													,$end
													,$message
													,$enable
													,$editorID )
		{
			$query = 'UPDATE '.configs::TABLE_SERVERNOTICE." 
						SET 
							`SERVER_LOCK`='$serverLock',
							`START`='$start',
							`END`='$end',
							`MESSAGE`='$message',
							`ENABLE`='$enable',
							`EDITOR_ID`='$editorID',
							`EDITED_DATE`=NOW()
						WHERE 
							`SEQ`='$seq'
						LIMIT 1;";
			return $con->query( $query );
							
		}
		
		// 공지사항 모든 수(관리자용)
		public static function countNotice( DataBaseConnect $con )
		{
			$query = 'SELECT COUNT(SEQ) FROM '.configs::TABLE_SERVERNOTICE.';';
			$fetch = DataBaseConnect::fetch_assoc( $con->query( $query ) );
			
			return $fetch[ 'COUNT(SEQ)' ];
		}
		// 모든 목록 가져오기(관리자용)
		public static function selectAllNotices( DataBaseConnect $con,
												$page,
												$numPerPage )
		{
			$startIndex = max( ($page-1)*$numPerPage, 0 );
			$query = 'SELECT t1.*, t2.NICKNAME AS OWNER_NICKNAME 
						FROM 
							'.configs::TABLE_SERVERNOTICE.' AS t1
						JOIN 
							'.configs::TABLE_MEMBER." AS t2 
						ON
							t1.OWNER_ID = t2.SEQ 
						LIMIT 
							$startIndex,$numPerPage;";
			return $con->query( $query );
		}
		
		// 모든 단일 목록 가져오기
		public static function selectNotice( DataBaseConnect $con, $seq )
		{
			$startIndex = max( ($page-1)*$numPerPage, 0 );
			$query = 'SELECT t1.*, t2.NICKNAME AS OWNER_NICKNAME 
						FROM 
							'.configs::TABLE_SERVERNOTICE.' AS t1
						JOIN 
							'.configs::TABLE_MEMBER." AS t2 
						ON
							t1.OWNER_ID = t2.SEQ 
						WHERE
							t1.SEQ = $seq 
						LIMIT 1;";
			return $con->query( $query );
		}
		
		// 서버가 잠겨 있는지 확인
		public static function isServerLock( DataBaseConnect $con )
		{
			$query = 'SELECT * FROM '.configs::TABLE_SERVERNOTICE." 
						WHERE 
							`START`<= NOW() 
							AND 
							`END` >= NOW() 
							AND 
							`SERVER_LOCK`='1' 
							AND 
							`ENABLE`='1' 
						LIMIT
							1;";
			return $con->query( $query );
		}
		
		// 모든 공지 사항 가져오기(일반유저용) - 날짜상으로 끝나지 않고 활성상태만 체크
		public static function selectAllNoticesForClient( DataBaseConnect $con )
		{
			$startIndex = max( ($page-1)*$numPerPage, 0 );
			$query = 'SELECT t1.*, t2.NICKNAME AS OWNER_NICKNAME 
						FROM 
							'.configs::TABLE_SERVERNOTICE.' AS t1
						JOIN 
							'.configs::TABLE_MEMBER." AS t2 
						ON
							t1.OWNER_ID = t2.SEQ 
						WHERE 
							`END` >= NOW() 
							AND 
							`ENABLE`='1';";
			return $con->query( $query );
		}
		
		
		// 정치인 평점 계산
		public static function calculateModelAVG( DataBAseConnect $con )
		{
			// 모두 비우기
			$con->query( 'DELETE FROM '.configs::TABLE_MODELSTATAVG.';' );
			
			$query = 'INSERT INTO '.configs::TABLE_MODELSTATAVG.'( `M_CODE`, `S_CODE`, `AVG`, `COUNT` ) '.
						'SELECT  t1.M_CODE, t1.S_CODE, AVG( t1.POINT ), COUNT( t1.OWNER_ID ) FROM  '.configs::TABLE_MEMBERMODELSTAT.' AS t1 WHERE t1.POINT !="0" GROUP BY t1.M_CODE, t1.S_CODE;';
			return $con->query( $query );
		}
		
		// 정치인 능력치 코드별 평점 조회
		public static function selectStatAVG( DataBaseConnect $con, $modelCode )
		{
			$query = 'SELECT S_CODE, AVG, COUNT FROM '.configs::TABLE_MODELSTATAVG." WHERE `M_CODE`=$modelCode;";
			return $con->query( $query );
		}
	}
?>